/**
 * Title:        Utility Class Library<p>
 * Description:  Class that captures and manages command line arguments
 *
 */
package com.timber.util;

import java.util.*;
import java.lang.IllegalArgumentException;
import java.io.*;
/**
  *    Class that captures and manages command line arguments
  *
  */

public class Arguments
{
  private Properties properties;
  private String[] commandLine;

  public Arguments( String[] legalParams, String[] commandLine )
  {
    this.commandLine = commandLine;

    // initialize properties
    properties = new Properties();

    Properties tempProp = new Properties();

    // setup properties with legal Parameters
    for( int i = 0; i < legalParams.length; ++i )
    {
      tempProp.put( legalParams[ i ], "" );
    }

    // read command line
    if( commandLine != null )
      for( int i = 0; i < commandLine.length; ++i )
      {
	// case insensitive
	String opt = commandLine[ i ].toUpperCase();

	if( opt.startsWith( "-" ) )
	{
	  if( tempProp.containsKey( opt ) )
	  {
	    if( ( i >= commandLine.length - 1 ) ||
		( commandLine[ i + 1 ].toUpperCase().startsWith(  "-" ) ) )
	    {
	      properties.put( opt, "" );
	      //Util.debug( "Setting property: " + opt + " value: \"\"" );
	    }
	    else
	    {
	      properties.put( opt, commandLine[ i + 1 ] );
	      //Util.debug( "Setting property: " + opt + " value: " + commandLine[ i + 1 ] );
	    }
	  }
	  else
	    throw new IllegalArgumentException( "Illegal parameter " + opt );
	}
      }
  }


  /**
   * Return an enumeration of the names of the arguments that have been specified.
   */
  public Enumeration argumentNames()
  {
    return properties.propertyNames();
  }


  /**
   * Get the value of a named argument, for example getArgument("-U")
   */
  public String getArgument( String prop )
  {
    return properties.getProperty( prop );
  }



  /**
   **  Set the value of a named argument, for example setArgument( "-U", "MyName" )
   **/
  public void setArgument( String arg, String prop )
  {
    properties.put( arg, prop );
  }

  /**
   * Return the original commandline
   */
  public String[] getCommandLine()
  {
    return commandLine;
  }

  public void logArguments() throws IOException
  {
    //properties.store( Global.getLogStream(), "Command Line Args" );
  }

}
